<?php include_partial('back/show', array('uri' => '@pointsCalculation')); ?>
<h1 class="title">Calcul de points effectué sur vos matchs</h1>

<div class="box_relief blue">
    N'hésitez pas à vous inscrire sur le site afin de garder en mémoire vos résultats et ainsi visualiser votre évolution, matchs joués, etc...
    <p><?php echo link_to("&#187; S'inscrire", 'sfApply/apply', array('class' => 'register')); ?></p>
</div>

<?php $coefficient = !empty($matchType['coefficient']) ? $matchType['coefficient'] : 1; ?>
<ul class="infosPointsCalcul">
    <li>Type de compétition : <?php echo !empty($matchType['title']) ? $matchType['title'] : 'Non défini' ?></li>
    <li>Coëfficient de la compétition : <?php echo number_format($coefficient, 2) ?></li>
    <li>Vos points mensuels : <?php echo $mensual_points ?></li>
</ul>

<h2 class="title">Liste des matchs joués</h2>

<table>
    <thead>
        <tr>
            <th class="first">N° licence</th>
            <th>Adversaire</th>
            <th>Nombre de points</th>
            <th>Vic./Déf.</th>
            <th>Gain</th>
            <th>Coeff.</th>
            <th class="last">Résultat</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $totalResultGain = 0;
        $resultGain = 0;
        $i = 0;
        $hasFillOpponentPoint = false;
        foreach($results as $result){
            if(!empty($result[ 'opponent_point' ])){
                $hasFillOpponentPoint = true;
                $gain = LicenseePoint::getGain($mensual_points, $result['opponent_point'], $scoring_table, $result['status']);
            ?>
            <tr class="l_<?php echo $i%2 ?>">
                <td class="first"><?php echo $result['opponent_licensee_number'] ?></td>
                <td><?php echo $result['opponent_lastname'] . " " . $result['opponent_firstname'] ?></td>
                <td><?php echo $result['opponent_point'] ?></td>
                <td><?php echo ($result['status'] == 1) ? 'Victoire' : 'Défaite' ?></td>
                <td><?php echo number_format($gain, 2) ?></td>
                <td><?php echo number_format($coefficient, 2) ?></td>
                <td class="last">
                    <?php
                    $resultGain = $gain * $coefficient;
                    $totalResultGain += $resultGain;
                    if($resultGain > 0){ ?>
                        <span class='victory'>+<?php echo number_format($resultGain, 2) ?></span>
                    <?php
                    }else if($resultGain < 0){
                    ?>
                        <span class='defeat'><?php echo number_format($resultGain, 2) ?></span>
                    <?php
                    }else{
                    ?>
                        <span>+<?php echo number_format($resultGain, 2); ?></span>
                    <?php }?>
                </td>
            </tr>
            <?php
            $i++;
            }
        }

        if(!$hasFillOpponentPoint){ ?>
            <tr class="l_0">
                <td colspan="7">Veuillez remplir au moins un adversaire</td>
            </tr>
        <?php } ?>
    </tbody>
    <tfoot>
        <tr>
            <th colspan="2">Total :</th>
            <th><?php echo $mensual_points + $totalResultGain ?></th>
            <th></th>
            <th></th>
            <th></th>
            <th class="last">
                <?php
                if($totalResultGain > 0){ ?>
                    <span class='victory'>+<?php echo number_format($totalResultGain, 2) ?></span>
                <?php
                }else if($resultGain < 0){
                ?>
                    <span class='defeat'><?php echo number_format($totalResultGain, 2) ?></span>
                <?php
                }else{
                ?>
                    <span>+<?php echo number_format($totalResultGain, 2); ?></span>
                <?php }?>
            </th>
        </tr>
    </tfoot>
</table>